package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Realm;
import com.ruoyi.system.service.IRealmService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 域名 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-10
 */
@Controller
@RequestMapping("/system/realm")
public class RealmController extends BaseController
{
    private String prefix = "system/realm";
	
	@Autowired
	private IRealmService realmService;
	
	@RequiresPermissions("system:realm:view")
	@GetMapping()
	public String realm()
	{
	    return prefix + "/realm";
	}
	
	/**
	 * 查询域名列表
	 */
	@RequiresPermissions("system:realm:list")
	@GetMapping("/list")
	@ResponseBody
	public List<Realm>  list(Realm realm)
	{

        List<Realm> list = realmService.selectRealmList(realm);
		return list;
	}
	
	
	/**
	 * 导出域名列表
	 */
	@RequiresPermissions("system:realm:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Realm realm)
    {
    	List<Realm> list = realmService.selectRealmList(realm);
        ExcelUtil<Realm> util = new ExcelUtil<Realm>(Realm.class);
        return util.exportExcel(list, "realm");
    }
	
	/**
	 * 新增域名
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存域名
	 */
	@RequiresPermissions("system:realm:add")
	@Log(title = "域名", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Realm realm)
	{		
		if(realm!=null&&realm.getRealmId()==null)
			realm.setRealmId(UUID.randomUUID()+"");
		return toAjax(realmService.insertRealm(realm));
	}

	/**
	 * 修改域名
	 */
	@GetMapping("/edit/{realmId}")
	public String edit(@PathVariable("realmId") String realmId, ModelMap mmap)
	{
		Realm realm = realmService.selectRealmById(realmId);
		mmap.put("realm", realm);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存域名
	 */
	@RequiresPermissions("system:realm:edit")
	@Log(title = "域名", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Realm realm)
	{		
		return toAjax(realmService.updateRealm(realm));
	}
	
	/**
	 * 删除域名
	 */
	@RequiresPermissions("system:realm:remove")
	@Log(title = "域名", businessType = BusinessType.DELETE)
	@GetMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(realmService.deleteRealmByIds(ids));
	}
	
}
